package com.guhecloud.step.definition.runtime.def;

import com.guhecloud.step.definition.runtime.*;
import com.guhecloud.step.definition.runtime.annotation.Partner;
import com.guhecloud.step.definition.runtime.tool.InternalAopUtils;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

@Getter
public class PartnerDef implements IRegistryAware {
    private String code;
    private String name;
    private IIdentityResolver partnerBean;


    private Map<Class<? extends IDomainExtension>, ExtensionDef> extensionDefMap = new HashMap<>();

    @Override
    public void registerBean(Object bean) {
        initialize(bean);
        InternalIndexer.index(this);
    }

    public ExtensionDef getExtension(Class<? extends IDomainExtension> extClazz) {
        return extensionDefMap.get(extClazz);
    }

    private void initialize(Object bean) {
        Partner partner = InternalAopUtils.getAnnotation(bean, Partner.class);
        this.code = partner.code();
        this.name = partner.name();
        this.partnerBean = (IIdentityResolver) bean;
    }

    public boolean match(@NotNull IDomainModel model) {
        return partnerBean.match(model);
    }

    public void registerExtensionDef(ExtensionDef extensionDef) {
        Class<? extends IDomainExtension> extClazz = extensionDef.getExtClazz();
        extensionDefMap.put(extClazz, extensionDef);
    }

}
